// pages/test/limit/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    itemNum:35,
    timer:null,
    timeClock:12,
    timeLimit:60,
    combo:0,
    right:0,
    point:0,
    total:0,
    preRight:false,
    classStr: ['selector', 'selector', 'selector', 'selector'],
    clicked:false,
    index:0,
    tempHistory:[],
    items:[
      {
        title:'おはよう',
        selector:[
          '你好','晚上好','再见','早上好'
        ],
        right:1
      }
    ]
  },
  getData(callback) {
    const items = getApp().getItem({
      itemNumber: this.data.itemNum,
      selectNum: 4
    }, (res) => {
      this.setData({
        items: res
      });
      if (callback) callback();
    });

  },
  giveUp() {
    clearInterval(this.data.timer);
    getApp().addHistory(this.data.tempHistory);
    getApp().setUserInfo({
      UUID: getApp().data.userInfo.openId,
      expSingle: getApp().data.userDetail.expSingle + this.data.point
    })
    getApp().data.userDetail.expSingle += this.data.point;
    wx.navigateBack({
      delta: 1,
    })
    
  },
  addHistory(title, result, type = 0) {
    this.data.tempHistory.push({
      UUID: getApp().data.userInfo.openId,
      title: title,
      result: result,
      type: type,
    })
  },
  click(e){
    const id=parseInt(e.target.id);
    if(!this.data.clicked){
      if(id===this.data.items[this.data.index].right){
        this.data.classStr[id-1]='right';
        this.data.right++;
        
        if(this.data.preRight){
          this.data.combo++;
          if(this.data.combo>10){
            this.data.timeClock+=5;
          }else if(this.data.combo>5){
            this.data.timeClock+=2;
          }
        }

        this.data.point += this.data.combo*5+parseInt(((this.data.index+1)/this.data.itemNum)*100);

        this.data.preRight=true;
        this.addHistory( this.data.items[this.data.index].title,1 )
      }else{
        this.data.point += 1;
        this.data.preRight=false;
        this.data.combo=0;
        this.data.classStr[id-1]='wrong';
        this.addHistory(this.data.items[this.data.index].title, 0 )
      }
      
      this.setData({
        point:this.data.point,
        combo:this.data.combo,
        right:this.data.right,
        total:this.data.total+1,
        clicked:true,
        classStr:[...this.data.classStr]
      })
      setTimeout(()=>{
        this.setData({
          classStr: ['selector', 'selector', 'selector', 'selector'],
          clicked:false,
          index:this.data.index+1
        })
        if (this.data.index ===this.data.itemNum){
          this.giveUp();
        }
      }, 500)
    }else{

    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  init(){
    this.setData({
      timeClock: this.data.timeLimit
    });
    if (!this.data.timer) {
      this.data.timer = clearInterval(this.data.timer);
    }
    const that = this;
    this.data.timer = setInterval(() => {
      that.setData({
        timeClock: that.data.timeClock - 1
      })
      if (that.data.timeClock <= 0) {
        this.giveUp();
      }
    }, 1000);
  },
  onShow: function () {
    this.getData(this.init);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.data.timer = clearInterval(this.data.timer);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.data.timer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})